#!/bin/bash

logFile="/tmp/setJbod.log"
MegaCons=(
    "LSI MegaRAID"
)
length=${#MegaCons[@]}

for ((i=0; i<$length; i++))
do
    echo "检查阵列卡型号: ${MegaCons[$i]}" >>$logFile
    search_result=$(lspci | grep "${MegaCons[$i]}")
    if [ ! -z "$search_result" ]; then
        echo "找到阵列卡型号：${MegaCons[$i]}" >>$logFile
        Enclosures=()
        Slots=()
        count=0
        /opt/MegaRAID/MegaCli/MegaCli64 -CfgForeign -Clear -aALL >>$logFile
        /opt/MegaRAID/MegaCli/MegaCli64 -CfgClr -aALL -NoLog >>$logFile
        /opt/MegaRAID/MegaCli/MegaCli64 -AdpSetProp -EnableJBOD -1 -aALL -NoLog >>$logFile
        #/opt/MegaRAID/MegaCli/MegaCli64 -PDList -aALL -NoLog
        for j in $(/opt/MegaRAID/MegaCli/MegaCli64 -PDList -aALL -NoLog | grep "Enclosure Device ID" | sed 's/ //g' | awk -F: '{print $2}'); do
          Enclosures+=($j)
          count=$((count + 1))
        done
        for j in $(/opt/MegaRAID/MegaCli/MegaCli64 -PDList -aALL -NoLog | grep "Slot Number" | sed 's/ //g' | awk -F: '{print $2}'); do
          Slots+=($j)
        done
        for j in $(seq 0 $((count - 1))); do
          Enclosure=${Enclosures[$j]}
          Slot=${Slots[$j]}
          #补充操作，如果FAILED可能上一步已经是JBOD硬盘了
          /opt/MegaRAID/MegaCli/MegaCli64 -PDMakeJBOD -PhysDrv "[$Enclosure:$Slot]" -aALL -NoLog >>$logFile
        done
    else
        echo "没有找到阵列卡型号：${MegaCons[$i]}" >>$logFile
    fi
done